package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * SubChoiceDataValid类
 * 实现数据有效性选择的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class SubChoiceDataValid implements AxdrType {

    private byte[] dataCode = null;
    private DataValidChoice choice = DataValidChoice._ERR_NONE_SELECTED;
    private MAC mac = null;
    private RN rn = null;
    private RN_MAC rn_mac = null;
    private SID sid = null;
    private SID_MAC sid_mac = null;

    public enum DataValidChoice {
        _ERR_NONE_SELECTED(-1),
        MAC(0),
        RN(1),
        RN_MAC(2),
        SID(3),
        SID_MAC(4);

        private final int value;

        DataValidChoice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static DataValidChoice fromValue(int value) {
            for (DataValidChoice choice : DataValidChoice.values()) {
                if (choice.value == value) {
                    return choice;
                }
            }
            return _ERR_NONE_SELECTED;
        }
    }

    public SubChoiceDataValid() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setMAC(MAC mac) {
        this.resetChoices();
        this.choice = DataValidChoice.MAC;
        this.mac = mac;
    }

    public void setRn(RN rn) {
        this.resetChoices();
        this.choice = DataValidChoice.RN;
        this.rn = rn;
    }

    public void setRnMac(RN_MAC rn_mac) {
        this.resetChoices();
        this.choice = DataValidChoice.RN_MAC;
        this.rn_mac = rn_mac;
    }

    public void setSID(SID sid) {
        this.resetChoices();
        this.choice = DataValidChoice.SID;
        this.sid = sid;
    }

    public void setSidMac(SID_MAC sid_mac) {
        this.resetChoices();
        this.choice = DataValidChoice.SID_MAC;
        this.sid_mac = sid_mac;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        com.dl698.asn1.axdr.AxdrEnum choosen = new com.dl698.asn1.axdr.AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = DataValidChoice.fromValue((int) choosen.getValue());

        switch (this.choice) {
            case MAC:
                this.mac = new MAC();
                codeLength += this.mac.decode(input);
                break;
            case RN:
                this.rn = new RN();
                codeLength += this.rn.decode(input);
                break;
            case RN_MAC:
                this.rn_mac = new RN_MAC();
                codeLength += this.rn_mac.decode(input);
                break;
            case SID:
                this.sid = new SID();
                codeLength += this.sid.decode(input);
                break;
            case SID_MAC:
                this.sid_mac = new SID_MAC();
                codeLength += this.sid_mac.decode(input);
                break;
            default:
                throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
        }

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            try {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
            } catch (java.io.IOException e) {
                throw new RuntimeException("Error writing dataCode to output stream", e);
            }
            return this.dataCode.length;
        }
        if (this.choice == DataValidChoice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        switch (this.choice) {
            case MAC:
                if (this.mac != null) {
                    codeLength += this.mac.encode(output);
                    com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                    c.setMinMaxVal(0);
                    codeLength += c.encode(output);
                } else {
                    throw new RuntimeException("mac is null");
                }
                break;
            case RN:
                if (this.rn != null) {
                    codeLength += this.rn.encode(output);
                    com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                    c.setMinMaxVal(1);
                    codeLength += c.encode(output);
                } else {
                    throw new RuntimeException("rn is null");
                }
                break;
            case RN_MAC:
                if (this.rn_mac != null) {
                    codeLength += this.rn_mac.encode(output);
                    com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                    c.setMinMaxVal(2);
                    codeLength += c.encode(output);
                } else {
                    throw new RuntimeException("rn_mac is null");
                }
                break;
            case SID:
                if (this.sid != null) {
                    codeLength += this.sid.encode(output);
                    com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                    c.setMinMaxVal(3);
                    codeLength += c.encode(output);
                } else {
                    throw new RuntimeException("sid is null");
                }
                break;
            case SID_MAC:
                if (this.sid_mac != null) {
                    codeLength += this.sid_mac.encode(output);
                    com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                    c.setMinMaxVal(4);
                    codeLength += c.encode(output);
                } else {
                    throw new RuntimeException("sid_mac is null");
                }
                break;
            default:
                throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
        }

        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding SubChoiceDataValid", e);
        }
        this.dataCode = revOStream.getArray();
    }

    public DataValidChoice getChoiceIndex() {
        return this.choice;
    }

    public MAC getMAC() {
        return this.mac;
    }

    public RN getRn() {
        return this.rn;
    }

    public RN_MAC getRnMac() {
        return this.rn_mac;
    }

    public SID getSID() {
        return this.sid;
    }

    public SID_MAC getSidMac() {
        return this.sid_mac;
    }

    public void resetChoices() {
        this.choice = DataValidChoice._ERR_NONE_SELECTED;
        this.mac = null;
        this.rn = null;
        this.rn_mac = null;
        this.sid = null;
        this.sid_mac = null;
    }

    @Override
    public String toString() {
        switch (this.choice) {
            case MAC:
                if (this.mac != null) {
                    return "choice: {mac: " + this.mac + "}";
                } else {
                    return "choice is mac but mac is null";
                }
            case RN:
                if (this.rn != null) {
                    return "choice: {rn: " + this.rn + "}";
                } else {
                    return "choice is rn but rn is null";
                }
            case RN_MAC:
                if (this.rn_mac != null) {
                    return "choice: {rn_mac: " + this.rn_mac + "}";
                } else {
                    return "choice is rn_mac but rn_mac is null";
                }
            case SID:
                if (this.sid != null) {
                    return "choice: {sid: " + this.sid + "}";
                } else {
                    return "choice is sid but sid is null";
                }
            case SID_MAC:
                if (this.sid_mac != null) {
                    return "choice: {sid_mac: " + this.sid_mac + "}";
                } else {
                    return "choice is sid_mac but sid_mac is null";
                }
            default:
                return "unknown";
        }
    }
}